package com.chinar.funds;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import java.util.concurrent.TimeUnit;

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            // 设备重启后重新设置定时任务
            scheduleSendMessageTask(context);
        }
    }

    private void scheduleSendMessageTask(Context context) {
        // 创建周期性工作任务，每15分钟执行一次
        PeriodicWorkRequest sendWork = new PeriodicWorkRequest.Builder(
                com.chinar.funds.SendMessageWorker.class, 15, TimeUnit.MINUTES)
                .build();

        // 添加任务到WorkManager
        WorkManager.getInstance(context).enqueueUniquePeriodicWork(
                "send_fund_message",
                ExistingPeriodicWorkPolicy.REPLACE,  // 统一使用REPLACE策略
                sendWork);
    }
}